//
//  PopoverAnimator.swift
//  weiboFirst
//
//  Created by ZhouRobert on 15/6/30.
//  Copyright © 2015年 ZhouRobert. All rights reserved.
//

import UIKit

//UIViewControllerTransitioningDelegate 提供转场动画相关的对象
class PopoverAnimator: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    var presentFrame = CGRectZero
    //是否展现的标记
    var isPresented = false
    
    //返回负责转场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        
        pc.presentFrame = presentFrame 
        
        return pc
    }
    
    //返回提供Model动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        return self
        
    }
    
    //返回提供Dismissed 动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        return self
    }
    
    //动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return isPresented ? 0.7 : 0.25
        
    }
    
    ///  转场动画实现函数 － 一旦实现这个函数，默认的动画失效，所有的动画由程序员提供
    ///
    ///  :param: transitionContext 转场上下文，提供了转场所需要的参数
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //判断一下是present or dismiss
        
        if isPresented {
            
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            //1. 将 toView添加到containerView
            transitionContext.containerView()?.addSubview(toView)
            
            //2. 动画实现
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            
            toView.layer.anchorPoint = CGPoint(x:0.5, y: 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                
                toView.transform = CGAffineTransformIdentity
                
                }, completion: { (_) -> Void in
                    //3. *** 结束动画 － 告诉系统转场动画已经结束
                    transitionContext.completeTransition(true)
                    
            })
            
        }else{
            
            // 1. 取消 － fromView 删除
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
            // 2. 结束动画－ 告诉系统转场动画已经结束
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                fromView.transform  = CGAffineTransformMakeScale(1.0, 0.0001)
                }, completion: { (_) -> Void in
                    
                    //2.1. 删除fromView(present的视图)
                     fromView.removeFromSuperview()
                    
                    //2.2. ***结束动画 — 告诉系统转场动画已经结束
                     transitionContext.completeTransition(true)

            })
        }
    }
}
